package cn.passlink.www.Exception;

import com.sun.mail.util.MailConnectException;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingRequestValueException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import java.sql.SQLSyntaxErrorException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@ControllerAdvice
public class CustomExceptionHandlers {

    //    请求体缺失异常
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public Map<String, Object> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        // 封装错误信息到Map对象中
        Map<String, Object> errorResponse = new HashMap<>();
        errorResponse.put("status", "403");
        errorResponse.put("message", "数据错误");
        log.error("NotReadableException", e);
        // 返回错误响应
        return errorResponse;
    }


    //    请求方式错误
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public Map<String, Object> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        // 封装错误信息到Map对象中
        Map<String, Object> errorResponse = new HashMap<>();
        errorResponse.put("status", "403");
        errorResponse.put("message", "请求方式错误");
        log.error("HttpRequestMethodNotSupportedException", e);
        // 返回错误响应
        return errorResponse;
    }


    //    请求参数错误
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public Map<String, Object> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        // 封装错误信息到Map对象中
        Map<String, Object> errorResponse = new HashMap<>();
        errorResponse.put("status", "403");
        errorResponse.put("message", "请求参数错误");
        // 返回错误响应
        log.error("MissingServletRequestParameterException", e);
        return errorResponse;
    }

    //    数据库参数操纵错误
    @ExceptionHandler(DataIntegrityViolationException.class)
    @ResponseBody
    public Map<String, Object> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
        // 封装错误信息到Map对象中
        Map<String, Object> errorResponse = new HashMap<>();
        errorResponse.put("status", "500");
        errorResponse.put("message", "服务器错，请联系管理员");
        // 返回错误响应
        log.error("MissingServletRequestParameterException", e);
        return errorResponse;

    }

    //文件大小错误
    @ExceptionHandler(SizeLimitExceededException.class)
    @ResponseBody
    public Map<String, Object> handleSizeLimitExceededException( SizeLimitExceededException e) {
        // 封装错误信息到Map对象中
        Map<String, Object> errorResponse = new HashMap<>();
        errorResponse.put("status", "403");
        errorResponse.put("message", "文件大小错误！！！");
        // 返回错误响应
        log.error("SizeLimitExceededException", e);
        return errorResponse;
    }

    //上传文件错误，请检查文件上传方式
    @ExceptionHandler(MissingServletRequestPartException.class)
    @ResponseBody
    public Map<String, Object> handleMissingServletRequestPartException(MissingServletRequestPartException e) {
        // 封装错误信息到Map对象中
        Map<String, Object> errorResponse = new HashMap<>();
        errorResponse.put("status", "403");
        errorResponse.put("message", "上传文件错误，请检查文件上传方式！！！");
        // 返回错误响应
        log.error("MissingServletRequestPartException", e);
        return errorResponse;
    }


    //数据库错误
    @ExceptionHandler(SQLSyntaxErrorException.class)
    @ResponseBody
    public Map<String, Object> handleSQLSyntaxErrorException(SQLSyntaxErrorException e) {
        // 封装错误信息到Map对象中
        Map<String, Object> errorResponse = new HashMap<>();
        errorResponse.put("status", "500");
        errorResponse.put("message", "数据库错误！！！");
        // 返回错误响应
        log.error("SQLSyntaxErrorException", e);
        return errorResponse;
    }

    //    邮箱发送失败
    @ExceptionHandler(MailConnectException.class)
    @ResponseBody
    public Map<String, Object> handleMailConnectException( MailConnectException e) {
        // 封装错误信息到Map对象中
        Map<String, Object> errorResponse = new HashMap<>();
        errorResponse.put("status", "500");
        errorResponse.put("message", "邮箱发送失败！！！");
        // 返回错误响应
        log.error("MailConnectException", e);
        return errorResponse;
    }

    //jwt过期
    @ExceptionHandler(ExpiredJwtException.class)
    @ResponseBody
    public Map<String, Object> handleExpiredJwtException( ExpiredJwtException e) {
        // 封装错误信息到Map对象中
        Map<String, Object> errorResponse = new HashMap<>();
        errorResponse.put("status", "403");
        errorResponse.put("message", "认证失败！！！");
        // 返回错误响应
        log.error("ExpiredJwtException", e);
        return errorResponse;
    }

    @ExceptionHandler(MultipartException.class)
    @ResponseBody
    public Map<String, Object> handleMultipartException(MultipartException e) {
        // 封装错误信息到Map对象中
        Map<String, Object> errorResponse = new HashMap<>();
        errorResponse.put("status", "500");
        errorResponse.put("message", "上传失败，请联系管理员！！！");
        // 返回错误响应
        log.error("MultipartException", e);
        return errorResponse;
    }


        @ExceptionHandler(MissingRequestValueException.class)
        @ResponseBody
        public Map<String, Object> handleMissingRequestValueException(MissingRequestValueException e) {
            // 封装错误信息到Map对象中
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("status", "403");
            errorResponse.put("message", "请求错误！！！");
            // 返回错误响应
            log.error("MissingRequestValueException", e);
            return errorResponse;
        }

    }